import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_methods.dart';

// 全局方法 - 弹出sheet
//
// sheet
// return 取消：null，其他可能为bool、num类型
showSheetDialog(
    {required Widget sheet,
    BuildContext? context,
    Color? barrierColor,
    Color? backgroundColor = DZColors.dialog,
    double cornerRadius = 10.0,
    bool enableDrag = true}) {
  return showModalBottomSheet(
    builder: (BuildContext context) => sheet,
    context: context ?? navigatorKey.currentContext!,
    barrierColor: barrierColor,
    backgroundColor: backgroundColor,
    enableDrag: enableDrag,
    clipBehavior: Clip.antiAlias,
    isScrollControlled: true,
    // 不然高度不能自定义
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.only(topLeft: Radius.circular(cornerRadius), topRight: Radius.circular(cornerRadius)),
    ),
  );
}

/// 带输入框，能抬起键盘
showInputSheetDialog({
  required Widget sheet,
  Color? barrierColor,
  Color? backgroundColor = DZColors.dialog,
  double cornerRadius = 10.0,
  bool enableDrag = true,
}) {
  return showModalBottomSheet(
    context: navigatorKey.currentContext!,
    backgroundColor: Colors.transparent,
    barrierColor: barrierColor,
    clipBehavior: Clip.antiAliasWithSaveLayer,
    enableDrag: enableDrag,
    isScrollControlled: true,
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.only(
        topLeft: Radius.circular(cornerRadius),
        topRight: Radius.circular(cornerRadius),
      ),
    ),
    builder: (BuildContext context) {
      final screenHeight = MediaQuery.of(context).size.height;
      return GestureDetector(
        onTap: () => FocusScope.of(context).requestFocus(FocusNode()),
        child: AnimatedPadding(
          padding: MediaQuery.of(context).viewInsets,
          curve: Curves.easeInToLinear,
          duration: const Duration(milliseconds: 0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              sheet,
              SizedBox(
                height: 0.0,
                child: OverflowBox(
                  alignment: Alignment.topCenter,
                  maxHeight: screenHeight,
                  child: Container(color: backgroundColor),
                ),
              ),
            ],
          ),
        ),
      );
    },
  );
}

showSmartSheet({
  required Widget sheet,
  String? tag,
  Color? barrierColor,
  Color? backgroundColor = DZColors.dialog,
  double cornerRadius = 10.0,
  bool hideWhenTouchOutside = true,
  bool safeAreaBottomDisable = true,
}) {
  return SmartDialog.show(
    tag: tag,
    alignment: Alignment.bottomCenter,
    clickMaskDismiss: hideWhenTouchOutside,
    maskColor: barrierColor,
    builder: (BuildContext context) {
      return Container(
        clipBehavior: Clip.antiAliasWithSaveLayer,
        padding: EdgeInsets.only(bottom: safeAreaBottomDisable ? MediaQuery.of(context).padding.bottom : 0.0),
        decoration: BoxDecoration(
          color: backgroundColor,
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(cornerRadius),
            topRight: Radius.circular(cornerRadius),
          ),
        ),
        child: sheet,
      );
    },
  );
}

// 基类
class XinDongBaseSheet extends StatelessWidget {
  final Widget? body; // 内容主体

  final Color? backgroundColor; // 背景颜色

  final bool safeAreaBottomDisable; // 底部安全区域占用

  final bool hideWhenTouchOutside; // 触摸外部、返回键时，是否隐藏Dialog，默认true

  // 构造函数
  const XinDongBaseSheet({
    Key? key,
    this.body,
    this.backgroundColor = DZColors.dialog,
    this.safeAreaBottomDisable = true,
    this.hideWhenTouchOutside = true,
  }) : super(key: key);

  // 确认调用方法
  void onConfirm(BuildContext context) {
    SmartDialog.dismiss(result: true);
  }

  // 取消调用方法
  void onCancel(BuildContext context) {
    SmartDialog.dismiss(result: null);
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      // 触摸外部、返回键时，是否隐藏Dialog
      onWillPop: () async => hideWhenTouchOutside,
      child: Material(
        type: MaterialType.transparency,
        child: Container(
          padding: EdgeInsets.only(bottom: safeAreaBottomDisable ? MediaQuery.of(context).padding.bottom : 0.0),
          color: backgroundColor,
          child: body,
        ),
      ),
    );
  }
}
